package is.glif.fenius.client.draft;

import static java.util.Arrays.asList;

import is.glif.fenius.client.draft.beans.ClientConfig;
import is.glif.fenius.util.ConfigManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ho.yaml.Yaml;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

public class Invoker {
    public static void main(String[] args) throws Exception {
        // create a parser
        OptionParser parser = new OptionParser() {
            {
                acceptsAll( asList( "h", "?" ), "show help then exit" );
                accepts( "help", "show extended help then exit" );
                accepts( "create", "do a (batched) create" );
                accepts( "rsid" , "Request set ID for create" ).withRequiredArg().describedAs("required").ofType(String.class);
                accepts( "list", "do a list" );
                accepts( "server" , "Server ID for list" ).withRequiredArg().describedAs("required").ofType(String.class);
                accepts( "cancel", "do a cancel" );
                accepts( "resvid" , "Reservation ID for cancel" ).withRequiredArg().describedAs("required").ofType(String.class);
            }
        };

        OptionSet options = parser.parse( args );

        // check for help
        if ( options.has( "?" ) || options.has("h") || options.has("help")) {
            parser.printHelpOn( System.out );
            System.exit(0);
        }
      
        
        Requester req = new Requester();
        boolean error = false;
        String errMsg = "";
        Requester.Operation op = null;
        
        if (options.has("create")) {
            op  = Requester.Operation.CREATE;
            if (options.has("rsid")) {
                String rsid = (String) options.valueOf("rsid");
                req.setRequestSetId(rsid);
            } else {
                errMsg = "No rsid for create";
                error = true;
            }
        } else if (options.has("list")) {
            op = Requester.Operation.LIST;
            if (options.has("server")) {
                String server = (String) options.valueOf("server");
                req.setServerId(server);
            } else {
                errMsg = "No server for list";
                error = true;
            }
            
        } else if (options.has("cancel")) {
            op = Requester.Operation.CANCEL;
            if (options.has("resvid")) {
                String resvid = (String) options.valueOf("resvid");
                req.setReservationId(resvid);
            } else {
                errMsg = "No resvsid for cancel";
                error = true;
            }
        }
        
        if (error) {
            System.err.println(errMsg);
            parser.printHelpOn( System.err );
            System.exit(1);
        }
        
        configureRequester(req);
        req.setOperation(op);
        req.start();
        
        
        ConsoleOutputter out = new ConsoleOutputter();
        out.printReservations(req.getReservations());
        
        
    }
    
    
    private static void configureRequester(Requester req) {
        try {
            ClientConfig clientConfig = Yaml.loadType(new File("config/config.yaml"), ClientConfig.class);
            req.setClientConfig(clientConfig);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
    
    
    }

}
